
/*****************************************************************************
*    Copyright (C)2003 Ali Corporation. All Rights Reserved.
*
*    File:    epg_demo.c
*
*    Description:    This file describes epg application design.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.	Sep.23.2003      Goliath Peng       Ver 0.1    Create file.
*     2.     Jan.19.2004      Goliath Peng       Ver 0,2     Modified the get section mechanism
*	3. 	Aug.12.2004 	   Firs Chen		Ver 1.0	 Modified the eit Data Structure and some api.
*	4. 	Apr.17.2006 	   Sunny Yin		Ver 1.1	 move from rock
*****************************************************************************/
#include <sys_config.h>

#include "com_epg.h"
//#include <api/libtsi/db_3l.h>
//#include <api/libtsi/sec_eit.h>
#include <api/libc/string.h>
//#include<api/libtsi/si_epg.h>
#include<api/libsi/lib_epg.h>
#include "system_data.h"
#include <osal/osal.h>
//#include <api/libosd/osd_common.h>
#include <api/libchar/lib_char.h>

#ifdef EPG_MULTI_LANG
static UINT8 g_current_lang_code[3];

void epg_set_lang_code(UINT8 lang_code[3])
{
	g_current_lang_code[0] = lang_code[0];
	g_current_lang_code[1] = lang_code[1];
	g_current_lang_code[2] = lang_code[2];
}
#endif

#if 0

#else
static char *content_nibble[16]=
{
	" ",
	"Movie",
	"News",
	"Show/Game",
	"Sports",
	"Childen's",
	"Music",
	"Arts/Culture",
	"Social/Eco",
	"Edu/Science",
	"Leisure",
	"Other",
	"Other",
	"Other",
	"Other",
	"Other"
};

static UINT8 epg_lang_select = 0;	//for multi-language
/*
 * lang_code2_select - user currently selected language
 * epg_lang_select is not recommended to use anymore
 */
static UINT16 lang_code2_select = 0xFFFF;

#define MAX_EVENT_NAME_LEN			64/*janzy@52 to 64*///52	//32		//Ben 131008#1
static UINT16 p_event_name[MAX_EVENT_NAME_LEN+1];
static UINT16 f_event_name[MAX_EVENT_NAME_LEN+1];
static UINT16 sch_event_name[MAX_EVENT_NAME_LEN+1];
UINT8* epg_get_event_name(eit_event_info_t *ep, INT32 *name_len)
{
	UINT8 *name;
	UINT8 len = 0;
	UINT32 refcode;
	UINT8 lang_select;
	
	if (ep==NULL)
		return NULL;

	lang_select = epg_get_event_language_select(ep);

	if (ep->event_type == PRESENT_EVENT)
	{
//		thai_ttc_print("%s,event_type=PRESENT_EVENT\n",__FUNCTION__);		
		name = (UINT8*)p_event_name;
	}
	else if (ep->event_type == FOLLOWING_EVENT)
	{
//		thai_ttc_print("%s,event_type=FOLLOWING_EVENT\n",__FUNCTION__);		
		name = (UINT8*)f_event_name;
	}
	else
	{
//		thai_ttc_print("%s,event_type=ELSE \n",__FUNCTION__);
		name = (UINT8*)sch_event_name;
	}

	//select language
#ifdef EPG_MULTI_LANG
	for (epg_lang_select=0; epg_lang_select<MAX_EPG_LANGUAGE_COUNT; epg_lang_select++)
	{
		if (compare_iso639_lang(lang2tolang3(ep->lang[epg_lang_select].lang_code2), g_current_lang_code) == 0)
			break;
	}

	if (epg_lang_select >= MAX_EPG_LANGUAGE_COUNT)
		epg_lang_select = 0;
#endif

	if (ep->lang[lang_select].text_char == NULL)
	{
		dvb_to_unicode("No Name", 7, (UINT16 *)name, MAX_EVENT_NAME_LEN, 1);
		return name;
	}


    if(NULL != ep->lang[lang_select].text_char)
	    len = ep->lang[lang_select].text_char[0];
	
	
	if (len > 0)
	{
		refcode = get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2));
		
	    *name_len = dvb_to_unicode(	ep->lang[lang_select].text_char+1, 
									len, 
									(UINT16*)name, 
									MAX_EVENT_NAME_LEN,
									refcode);  
		return name;
	}
	else
	{
		dvb_to_unicode("No Name", 7, (UINT16 *)name, MAX_EVENT_NAME_LEN, 1);
		return name;
	}
}

static INT32 ConvertDateTimeToString(date_time *dt, UINT8* str)
{
	UINT8 hour,min;
	INT32 hour_off,min_off,sec_off;
	date_time tmp;

	SYSTEM_DATA * sys_data;
	
	get_STC_offset(&hour_off,&min_off,&sec_off);

	convert_time_by_offset(&tmp, dt,  hour_off, min_off);

	hour = tmp.hour;
	min = tmp.min;

 	sprintf(str, "%02d-%02d %02d:%02d",tmp.month, tmp.day, hour, min);
 	return 5;
}

INT32 epg_get_event_start_time(eit_event_info_t *ep,UINT8* str)
{
	date_time dt;

	if (ep==NULL)
		return 0;

	get_event_start_time(ep, &dt);

	return ConvertDateTimeToString(&dt, str);
}

INT32 epg_get_event_end_time(eit_event_info_t *ep,UINT8* str)
{
	date_time dt;

	if (ep==NULL)
		return 0;

	get_event_end_time(ep, &dt);
	
	return ConvertDateTimeToString(&dt, str);
}

UINT8 *epg_get_event_language(eit_event_info_t *ep)
{
	UINT8 lang_select;
	if (NULL==ep)
		return NULL;

	lang_select = epg_get_event_language_select(ep);
	return lang2tolang3(ep->lang[lang_select].lang_code2);
}
UINT8 epg_get_event_language_count(eit_event_info_t *ep)
{
	UINT8 i, count = 0;
	if (ep == 0)
		return 0;
	for (i=0; i<3; i++) 
		if (ep->lang[i].lang_code2 != 0)
			count++;
	return count;	
}

UINT8 epg_get_event_language_select(eit_event_info_t *ep)
{
	UINT8 i;
	if (lang_code2_select == 0xFFFF)
		return 0;
	
	for (i=0; i<MAX_EPG_LANGUAGE_COUNT; i++)
		if (ep->lang[i].lang_code2 == lang_code2_select)
			return i;
		
	return 0;
}

void epg_set_event_langcode_select(UINT16 lang_code2)
{
	lang_code2_select = lang_code2;
}

INT32 epg_get_event_content_type(eit_event_info_t* ep,UINT8 *str,INT32 *len)
{
	if (ep==NULL) return !SUCCESS;

	STRCPY(str, content_nibble[ep->nibble]);
	*len=STRLEN(content_nibble[ep->nibble]);
	str[*len]='\0';

	return SUCCESS;
}

UINT32 epg_get_event_all_short_detail(eit_event_info_t *ep,UINT16 *short_buf,UINT32 maxLen)
{
	UINT8* text;
	UINT16 len;
	UINT8 lang_select;

	if (ep==NULL)	return 0;

	lang_select = epg_get_event_language_select(ep);
	if (ep->lang[lang_select].text_char == NULL)	return 0;

	len = ep->lang[lang_select].text_char[0];			//event name
	text = ep->lang[lang_select].text_char + len + 1;	//short text char
	len = text[0];
	if (len > 0)
	{
		return dvb_to_unicode(text+1, len, short_buf, maxLen,
							get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2)));
	}
	else
		return 0;
}
UINT32 epg_get_event_all_extented_detail(eit_event_info_t *ep,UINT16 *extented_buf,UINT32 maxLen)
{
	UINT8* text;
	UINT16 len;
	UINT8 lang_select;

	if (ep==NULL)	return 0;
	lang_select = epg_get_event_language_select(ep);

	if (ep->lang[lang_select].text_char == NULL)	return 0;
	
#ifdef EPG_MULTI_TP
	len = ep->lang[lang_select].ext_text_length;
	text = ep->lang[lang_select].ext_text_char;
	if (len > 0)
	{
		return dvb_to_unicode(text, len, extented_buf, maxLen,
							get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2)));
	}
	else
		return 0;
#else // for multi tp epg, by davy
	len = ep->lang[lang_select].text_char[0];			//event name
	text = ep->lang[lang_select].text_char + len + 1;	//short text char
	len = text[0];
	text += (len + 1);				//extended text char
	len = (text[0]<<8) | text[1];
	if (len > 0)
	{
		return dvb_to_unicode(text+2, len, extented_buf, maxLen,
							get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2)));
	}
	else
		return 0;
#endif
}

eit_event_info_t* epg_get_cur_service_event(INT32 prog_idx, UINT32 event_type,date_time *start_dt,date_time *end_dt,INT32 *event_num, BOOL update)
{
	P_NODE service_info;
	date_time cur_dt;

	if (event_num != NULL)
		*event_num = 0;

	if (SUCCESS==get_prog_at(prog_idx, &service_info))
	{
#ifdef EPG_MULTI_TP
		get_local_time( &cur_dt );
		epg_set_select_time(&cur_dt, start_dt, end_dt);
#endif
		return epg_get_service_event(service_info.tp_id, service_info.prog_number,
									event_type, start_dt, end_dt, event_num, update);
	}
	else
		return NULL;
}
#endif
